package com.xhg.common;


import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.render.ViewType;
import com.xhg.controller.AdminController;
import com.xhg.controller.MainController;
import com.xhg.controller.PowerController;
import com.xhg.controller.UserController;
import com.xhg.model.Power;
import com.xhg.model.RolePower;
import com.xhg.model.User;

public class WebConfig extends JFinalConfig {
    /**
     * 供Shiro插件使用。
     */
    Routes routes;
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		loadPropertyFile("config/database.properties");// 加载少量必要配置，随后可用getProperty(...)获取值
		me.setDevMode(getPropertyToBoolean("devMode", true));
		me.setViewType(ViewType.JSP);
		me.setBaseViewPath("WEB-INF/view");//定义视图路径
		
		me.setErrorView(401, "WEB-INF/view/error/401.jsp");//未授权
		me.setErrorView(403, "WEB-INF/view/error/403.jsp");
		me.setError404View("WEB-INF/view/error/404.jsp");//未找到资源
		me.setError500View("WEB-INF/view/error/500.jsp");
	}
	/**
     * 配置路由
     */
	public void configRoute(Routes me) {
		this.routes = me;
	        
		me.add("/", MainController.class);
		me.add("/admin", AdminController.class,"/commons");
		me.add("/user", UserController.class);
		me.add("/power",PowerController.class);
	}
	/**
	 * mysql数据库配置(配置插件)
	 */
	public void configPlugin(Plugins me) {
	     // 配置C3p0数据库连接池插件
        C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"),getProperty("user"),getProperty("password").trim());
        me.add(c3p0Plugin);
        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
        me.add(arp);
        arp.addMapping("y_user", User.class); // 映射y_user 表到 user模型
        arp.addMapping("y_power",Power.class);
        arp.addMapping("y_rolepower",RolePower.class);
        
        
        // 加载配置Shiro插件
        ShiroPlugin shiro=new ShiroPlugin(routes);
        me.add(shiro);
	}
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		//3、申明式事务
//		me.add(new TxByRegex(".*insert.*"));//正则表达式匹配action开启事务
//		me.add(new TxByActionKeys("/user/insert", "/user/save","","....."));//对指定actionKey进行拦截开始事务
//		me.add(new TxByActionMethods("insert","","...."));//对指定的metHod进行拦截并开启事务。
		
		/**
		 * 添加shiro的拦截器
		 */
		me.add(new ShiroInterceptor());
	}
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		
	}
}